package com.shenfa.action.systemManager;

import java.util.List;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shenfa.bean.Depart;
import com.shenfa.service.systemManager.depart.DepartService;
import com.shenfa.util.ResultObject;
import com.shenfa.util.sequence.SequenceUtil;
/**
 * 部门
 * @author jiangmaochun
 *
 */
public class DepartAction extends ActionSupport implements ModelDriven<Depart>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private DepartService departService;
	@Autowired
	private SequenceUtil sequenceUtil;
	
	private static Logger log=Logger.getLogger(DepartAction.class);
	private int start;
	private int limit;
	private String depname;
	
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public JSONObject result;
	
	Depart depart = new Depart();

	public void setDepart(Depart depart) {
		this.depart = depart;
	}

	public Depart getModel() {
		return depart;
	}

	public String getDepname() {
		return depname;
	}

	public void setDepname(String depname) {
		this.depname = depname;
	}

	/**
	 * 得到所有部门
	 * @return
	 */
	public String lsitAll(){
		List<Depart> list = departService.getAll();
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 获取机构下所有部门
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByOrg(){
		log.debug("start="+start+",limit="+limit+",Oid="+depart.getOid());
		List<Depart> list = departService.getAllByOrg(depart.getOid(),start,limit);
		result = ResultObject.build("0", list);
		result.put("total",departService.queryCount(depart.getOid()));
		return SUCCESS;
	}
	
	public String add(){
		log.debug("name="+depart.getName()+",Oid="+depart.getOid()+",remark="+depart.getRemark()+".state="+depart.getState());
		String id=sequenceUtil.getSequence(10);
		depart.setId(id);
		departService.add(depart);
		return backResult(true,"部门    "+depart.getName()+"添加成功");
	}
	
	/**
	 * 修改某部门
	 * @return
	 */
	public String update(){
		log.debug("name="+depart.getName()+",Oid="+depart.getOid()+",remark="+depart.getRemark()+".state="+depart.getState());
		departService.update(depart);
		return backResult(true,"部门    "+depart.getName()+"修改成功");
	}
	
	/**
	 * 删除部门
	 * @return
	 */
	public String delete(){
		log.debug("id="+depart.getId());
		departService.delete(depart.getId());
		return SUCCESS;
	}
	
	/**
	 * 公共返回方法
	 * @param isTure
	 * @param msg
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String backResult(Boolean isTure,String msg){
		if(null==result){
			result = new JSONObject();
		}
		result.put("success", isTure);
		result.put("msg",msg);
		return SUCCESS;
	}
	/**
	 * 验证某一机构下部门名是否被占用
	 * @return
	 */
	public String checkDep(){
		depart.setName(depname);
		int flag = departService.check(depart);
		if(flag == 0){
			result = ResultObject.build("0", null);//0表示不存在，可以使用
		}else{
			result = ResultObject.build("1", null);//1表示已经被占用
		}
		return SUCCESS;
	}
}